package com.mindainfo.groundwave.server.member.dao.impl;

import com.mindainfo.groundwave.server.base.dao.impl.BaseDaoImpl;
import com.mindainfo.groundwave.server.member.bean.SysDept;
import com.mindainfo.groundwave.server.member.dao.IDeptDao;
import org.mongodb.morphia.query.FindOptions;
import org.mongodb.morphia.query.Query;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Created by MD-01 on 2017/5/15.
 */
@Repository
public class DeptDaoImpl extends BaseDaoImpl<SysDept> implements IDeptDao{

    public SysDept queryByName(String name){
        return getDatastore().createQuery(SysDept.class)
                .field("name").equal(name)
                .get();
    }

    public List<SysDept> queryList(int start, int num, String key){

        Query<SysDept> query = getDatastore().createQuery(SysDept.class);
        if(!StringUtils.isEmpty(key)){
            query.field("name").contains(key);
        }
        FindOptions options = new FindOptions();
        options.skip(start).limit(num);
        return query.order("orderNum").asList(options);
    }

    public int queryListCount(String key){
        Query<SysDept> query = getDatastore().createQuery(SysDept.class);
        if(!StringUtils.isEmpty(key)){
            query.field("name").contains(key);
        }
        return (int)query.count();
    }

    @Override
    public List<SysDept> queryUsableAll() {
        return getDatastore().createQuery(SysDept.class)
                .field("usable").equal(true)
                .order("orderNum").asList();
    }
}
